//
//  NavHeader.m
//  linlee-iotios
//
//  Created by mengze on 2025/5/27.
//

#import <Masonry/Masonry.h>
#import "NavHeader.h"
#import "UIColor+Hex.h"

@interface NavHeader()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton * leftButton;
@property (nonatomic, strong) UIButton * rightButton;

@end

@implementation NavHeader  {
    BOOL _leftButtonHidden;
    BOOL _rightButtonHidden;
}


- (instancetype)init {
    
    
    self = [super init];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor systemBlueColor];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_leftButton setImage:[UIImage systemImageNamed:@"chevron.left"] forState:UIControlStateNormal];
    _leftButton.tintColor = [UIColor whiteColor];
    _leftButton.hidden = NO;
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    _rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_rightButton setTitle:@"完成" forState:UIControlStateNormal];
    _rightButton.tintColor = [UIColor whiteColor];
    _rightButton.hidden = NO;
    
    [self addSubview:_leftButton];
    [self addSubview:_titleLabel];
    [self addSubview:_rightButton];
}

- (void)setupConstraints {
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.centerY.equalTo(self);
        make.width.height.mas_equalTo(44);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.left.greaterThanOrEqualTo(_leftButton.mas_right).offset(8);
        make.right.lessThanOrEqualTo(_rightButton.mas_left).offset(-8);
    }];
    
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.centerY.equalTo(self);
        make.height.mas_equalTo(44);
    }];
}

#pragma mark - 显隐控制
- (void)setLeftButtonHidden:(BOOL)hidden {
    _leftButtonHidden = hidden;
    _leftButton.hidden = hidden;
    [self updateTitleLabelConstraints];
}

- (void)setRightButtonHidden:(BOOL)hidden {
    _rightButtonHidden = hidden;
    _rightButton.hidden = hidden;
    [self updateTitleLabelConstraints];
}

- (void)updateTitleLabelConstraints {
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        
        if (!_leftButtonHidden) {
            make.left.greaterThanOrEqualTo(_leftButton.mas_right).offset(8);
        } else {
            make.left.greaterThanOrEqualTo(self).offset(16);
        }
        
        if (!_rightButtonHidden) {
            make.right.lessThanOrEqualTo(_rightButton.mas_left).offset(-8);
        } else {
            make.right.lessThanOrEqualTo(self).offset(-16);
        }
    }];
}

@end

